<?php
////////////////////////////////////////////////////////////////////////////////
//   Copyright (C) 2004 ReloadCMS Development Team                            //
//   http://reloadcms.sf.net                                                  //
//                                                                            //
//   This program is distributed in the hope that it will be useful,          //
//   but WITHOUT ANY WARRANTY, without even the implied warranty of           //
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     //
//                                                                            //
//   This product released under GNU General Public License v2                //
////////////////////////////////////////////////////////////////////////////////
$filesdb = new linksdb(DOWNLOADS_DATAFILE);

// Creating new file
if(!empty($_POST['newsave'])){
    // Defining type of file
   if(!empty($_POST['type']) && $_POST['type'] == 1) {
       $_POST['link'] = basename($_POST['link']);
       $size = (int) @filesize(FILES_PATH . $_POST['link']);
   } else {
      $size = $_POST['size'];
   }
    // And trying to save it to database
    if($filesdb->createFile($_POST['newsave']-1, @$_POST['title'], @$_POST['desc'], @$_POST['link'], $size, @$_POST['author'], @$_POST['home'], @$_POST['licence'], @$_POST['screen'], @$_POST['platform'])) {
        rcms_showAdminMessage(__('File added'));
    } else rcms_showAdminMessage(__('Error occured'));
} elseif(!empty($_POST['delete']) && is_array($_POST['delete']) && !empty($_POST['cid'])) {
    $result = '';
    $cid = (int) $_POST['cid']-1;
    foreach ($_POST['delete'] as $fid => $cond){
        if(!empty($cond)){
            if($filesdb->deleteFile($cid, $fid)) {
                $result .= __('File removed') . ' (' . $cid . ':' . $fid . ')<br>';
            } else {
                $result .= __('Error occured') . ' (' . $cid . ':' . $fid . ')<br>';
            }
        }
    }
    rcms_showAdminMessage($result);
    unset($_POST['edit']);
} elseif (!empty($_POST['edit']) && !empty($_POST['save']) && !empty($_POST['cid'])){
    $_POST['edit'] = $_POST['edit'] - 1;
    if(!empty($_POST['type']) && $_POST['type'] == 1) {
        $_POST['link'] = basename($_POST['link']);
        $size = (int) @filesize(FILES_PATH . $_POST['link']);
    } else {
        $size = $_POST['size'];
    }
    if($filesdb->updateFile($_POST['cid']-1, $_POST['edit'], @$_POST['title'], @$_POST['desc'], @$_POST['link'], $size, @$_POST['author'], @$_POST['home'], @$_POST['licence'], @$_POST['screen'], @$_POST['platform'])) {
        rcms_showAdminMessage(__('File updated'));
    } else rcms_showAdminMessage(__('Error occured'));
}

////////////////////////////////////////////////////////////////////////////////
// Interface generation                                                       //
////////////////////////////////////////////////////////////////////////////////
if(!empty($_POST['new'])){
    $frm = new InputForm ('', 'post', '&lt;&lt;&lt; ' . __('Back')); $frm->hidden('cid', $_POST['new']); $frm->show();
    $frm = new InputForm ('', 'post', __('Submit'));
    $frm->addbreak(__('New file'));
    $frm->hidden('newsave', $_POST['new']);
    $frm->hidden('cid', $_POST['new']);
    $frm->addrow(__('Title'), $frm->text_box('title', ''));
    $frm->addrow(__('Platform'), $frm->text_box('platform', ''));
    $frm->addrow(__('Licence'), $frm->text_box('licence', ''));
    $frm->addrow(__('Author'), $frm->text_box('author', ''));
    $frm->addrow(__('Home Page'), $frm->text_box('home', 'http://'));
    $frm->addrow(__('Size of file'), $frm->text_box('size', ''));
    $frm->addrow(__('Description'), $frm->textarea('desc', ''));
    $frm->addrow(__('Screenshot - URL'), $frm->text_box('screen', ''));
    $frm->addrow(__('Filename or link to remote file'), $frm->text_box('link', ''));
    $frm->addrow(__('Select type of link you specified in previous field'), $frm->select_tag('type', array(__('You entered the link to remote file'), __('You entered filename of file uploaded through admin interface'))));
    $frm->show();
} elseif(!empty($_POST['edit']) && !empty($_POST['cid']) && !empty($filesdb->data[$_POST['cid']-1]['files'][$_POST['edit']-1])){
    $cid = $_POST['cid'] - 1;
    $fid = $_POST['edit'] - 1;
    $frm = new InputForm ('', 'post', '&lt;&lt;&lt; ' . __('Back')); $frm->hidden('cid', $_POST['cid']); $frm->show();
    $frm = new InputForm ('', 'post', __('Submit'));
    $frm->addbreak(__('Edit file'));
    $frm->hidden('save', '1');
    $frm->hidden('edit', $_POST['edit']);
    $frm->hidden('cid', $_POST['cid']);
    $frm->addrow(__('Title'), $frm->text_box('title', $filesdb->data[$cid]['files'][$fid]['name']));
    $frm->addrow(__('Description'), $frm->textarea('desc', $filesdb->data[$cid]['files'][$fid]['desc']));
    $frm->addrow(__('Platform'), $frm->text_box('platform', $filesdb->data[$cid]['files'][$fid]['platform']));
    $frm->addrow(__('Licence'), $frm->text_box('licence', $filesdb->data[$cid]['files'][$fid]['licence']));
    $frm->addrow(__('Author'), $frm->text_box('author', $filesdb->data[$cid]['files'][$fid]['author']));
    $frm->addrow(__('Home Page'), $frm->text_box('home', $filesdb->data[$cid]['files'][$fid]['home']));
    $frm->addrow(__('Size of file'), $frm->text_box('size', $filesdb->data[$cid]['files'][$fid]['size']));
    $frm->addrow(__('Screenshot'), $frm->text_box('screen', $filesdb->data[$cid]['files'][$fid]['screen']));
    $frm->addrow(__('Filename or link to remote file'), $frm->text_box('link', $filesdb->data[$cid]['files'][$fid]['link']));
    $frm->addrow(__('Select type of link you specified in previous field'), $frm->select_tag('type', array(__('You entered the link to remote file'), __('You entered filename of file uploaded through admin interface'))));
    $frm->show();
} elseif(!empty($_POST['cid'])) {
    $frm = new InputForm ('', 'post', '&lt;&lt;&lt; ' . __('Back')); $frm->show();
    $frm = new InputForm ('', 'post', __('New file')); $frm->hidden('new', $_POST['cid']); $frm->show();
    if(!empty($filesdb->data[$_POST['cid']-1]['files'])){
        $frm = new InputForm ('', 'post', __('Submit'));
        $frm->resetButton(__('Reset'));
        $frm->hidden('cid', $_POST['cid']);
        foreach ($filesdb->data[$_POST['cid']-1]['files'] as $fid => $fdata){
            $frm->addrow($fdata['link'] . '<br>' . $fdata['name'] . ' (' . $fdata['desc'] . '). ' . __('Size of file') . ': ' . $fdata['size'],
                $frm->checkbox('delete[' . $fid . ']', '1', __('Delete')) . ' ' .
                $frm->radio_button('edit', array($fid+1 => __('Edit')), 0), 'top'
            );
        }
        $frm->show();
   }
} else {
    $clist = array();
    foreach ($filesdb->data as $cid => $cdata) $clist[$cid+1] = $cdata['name'];
    if(!empty($clist)){
        $frm =new InputForm ('', 'post', __('Browse'));
        $frm->addrow(__('Select category'), $frm->select_tag('cid', $clist));
        $frm->show();
    } else rcms_showAdminMessage( __('There is no categories there'));
}
$filesdb->close();
?>
